#import "CellFrame.h"
#import "CellDataModel.h"
#import "ScreenAdapter.h"

#define NameFont [UIFont systemFontOfSize:15]
#define TitleFont [UIFont systemFontOfSize:16]
#define TextFont [UIFont systemFontOfSize:16]
#define TIMETextFont [UIFont systemFontOfSize:10]
#define PLUGIN_TITLE_FRAME_HEIGHT 26

@implementation CellFrame

//设置cell模型的布局
- (void)setModel:(CellDataModel *)dataModel
{
    ScreenAdapter *sa = [ScreenAdapter getInstanse];
    _model = dataModel;
    CGFloat bottomLine = 0;
    CGFloat temp_bottomLine = 0;
    // 间隙
    CGFloat padding = 10;
    
    // 设置头像的frame
    CGFloat iconViewX = padding;
    CGFloat iconViewY = padding;
    CGFloat iconViewW = 33;
    CGFloat iconViewH = 33;
    CGFloat picW = (APP_SCREEN_WIDTH - 85) / 3;
    self.iconF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
    
    //设置昵称的frame
    CGFloat nameLabelX = CGRectGetMaxX(self.iconF) + padding;
    CGFloat nameLabelY = 2 * padding;
    CGFloat nameLabelW = sa.screenWidth - nameLabelX;
    CGSize nameSize = [self sizeWithString:_model.nickname font:NameFont maxSize:CGSizeMake(nameLabelW, MAXFLOAT)];
    CGFloat nameLabelH = nameSize.height;
    self.nameF = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
    
    bottomLine = CGRectGetMaxY(self.nameF);
    if ([_model.type integerValue] == 5) { //布置作业的界面
        if ([_model.plugin1 count] != 0) {
            CGFloat plugin01X = nameLabelX;
            CGFloat plugin01Y = CGRectGetMaxY(self.nameF)+padding;
            CGFloat plugin01W = picW;
            CGFloat plugin01H = picW;
            self.plugin01F = CGRectMake(plugin01X, plugin01Y, plugin01W, plugin01H);
            self.plugin01ButtonF = CGRectMake(plugin01X, plugin01Y, plugin01W, plugin01H);
            self.title01F = CGRectMake(plugin01X, plugin01Y,plugin01W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin01F);
        }
        
        if ([_model.plugin2 count] != 0) {
            CGFloat plugin02X = CGRectGetMaxX(self.plugin01F)+padding;
            CGFloat plugin02Y = CGRectGetMaxY(self.nameF)+padding;
            CGFloat plugin02W = picW;
            CGFloat plugin02H = picW;
            self.plugin02F = CGRectMake(plugin02X, plugin02Y, plugin02W, plugin02H);
            self.plugin02ButtonF = CGRectMake(plugin02X, plugin02Y, plugin02W, plugin02H);
            self.title02F = CGRectMake(plugin02X, plugin02Y,plugin02W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin01F);
        }
        
        if ([_model.plugin3 count] != 0) {
            CGFloat plugin03X = CGRectGetMaxX(self.plugin02F)+padding;
            CGFloat plugin03Y = CGRectGetMaxY(self.nameF)+padding;
            CGFloat plugin03W = picW;
            CGFloat plugin03H = picW;
            self.plugin03F = CGRectMake(plugin03X, plugin03Y, plugin03W, plugin03H);
            self.plugin03ButtonF = CGRectMake(plugin03X, plugin03Y, plugin03W, plugin03H);
            self.title03F = CGRectMake(plugin03X, plugin03Y,plugin03W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin01F);
            bottomLine = CGRectGetMaxY(self.plugin01F);
        }
        
        if ([_model.plugin4 count] != 0) {
            CGFloat plugin04X = nameLabelX;
            CGFloat plugin04Y = CGRectGetMaxY(self.plugin03F)+padding;
            CGFloat plugin04W = picW;
            CGFloat plugin04H = picW;
            self.plugin04F = CGRectMake(plugin04X, plugin04Y, plugin04W, plugin04H);
            self.plugin04ButtonF = CGRectMake(plugin04X, plugin04Y, plugin04W, plugin04H);
            self.title04F = CGRectMake(plugin04X, plugin04Y,plugin04W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin04F);
        }
        
        if ([_model.plugin5 count] != 0) {
            CGFloat plugin05X = CGRectGetMaxX(self.plugin04F)+padding;
            CGFloat plugin05Y = CGRectGetMaxY(self.plugin03F)+padding;
            CGFloat plugin05W = picW;
            CGFloat plugin05H = picW;
            self.plugin05F = CGRectMake(plugin05X, plugin05Y, plugin05W, plugin05H);
            self.plugin05ButtonF = CGRectMake(plugin05X, plugin05Y, plugin05W, plugin05H);
            self.title05F = CGRectMake(plugin05X, plugin05Y,plugin05W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin04F);
        }
        
        if ([_model.plugin6 count] != 0) {
            CGFloat plugin06X = CGRectGetMaxX(self.plugin05F)+padding;
            CGFloat plugin06Y = CGRectGetMaxY(self.plugin03F)+padding;
            CGFloat plugin06W = picW;
            CGFloat plugin06H = picW;
            self.plugin06F = CGRectMake(plugin06X, plugin06Y, plugin06W, plugin06H);
            self.plugin06ButtonF = CGRectMake(plugin06X, plugin06Y, plugin06W, plugin06H);
            self.title06F = CGRectMake(plugin06X, plugin06Y,plugin06W, PLUGIN_TITLE_FRAME_HEIGHT);
            temp_bottomLine = CGRectGetMaxY(self.plugin04F);
            bottomLine = CGRectGetMaxY(self.plugin04F);
        }
        
        if (temp_bottomLine != 0 ) {
            bottomLine = temp_bottomLine;
        }
        
    }else{
    
        //设置标题的frame
        CGFloat titleX = nameLabelX;
        CGFloat titleY = CGRectGetMaxY(self.nameF)+padding;
        CGFloat titleW = sa.screenWidth-padding-nameLabelX;
        CGSize titleTextSize =  [self sizeWithString:_model.title font:TitleFont maxSize:CGSizeMake(titleW, MAXFLOAT)];
        CGFloat titleH = titleTextSize.height;
        self.titleF = CGRectMake(titleX, titleY, titleW, titleH);
    
        // 设置正文的frame
        CGFloat contentX = nameLabelX;
        CGFloat contentY = CGRectGetMaxY(self.titleF)+padding;
        CGFloat contentW = sa.screenWidth-padding-nameLabelX;
        CGSize textSize =  [self sizeWithString:_model.content font:TextFont maxSize:CGSizeMake(contentW, MAXFLOAT)];
        CGFloat contentH = textSize.height;
        self.contentF = CGRectMake(contentX, contentY, contentW, contentH);
    
        bottomLine = CGRectGetMaxY(self.contentF);
    
        // 设置配图的frame
        if (_model.image1 != nil && ![_model.image1 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = nameLabelX;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img01F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img01ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img01F);
        }
        // 设置配图的frame
        if (_model.image2 != nil && ![_model.image2 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img01F)+padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img02F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img02ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img01F);
        }
    
        // 设置配图的frame
        if (_model.image3 != nil && ![_model.image3 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img02F)+padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img03F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img03ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img01F);
            bottomLine = CGRectGetMaxY(self.img01F);
        }
    
        // 设置配图的frame
        if (_model.image4 != nil && ![_model.image4 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = nameLabelX;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img04F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img04ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img04F);
        }
    
        // 设置配图的frame
        if (_model.image5 != nil && ![_model.image5 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img04F) + padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img05F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img05ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img04F);
        }
    
        // 设置配图的frame
        if (_model.image6 != nil && ![_model.image6 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img05F) + padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img06F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img06ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img04F);
            bottomLine = CGRectGetMaxY(self.img04F);
        }
        
        // 设置配图的frame
        if (_model.image7 != nil && ![_model.image7 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = nameLabelX;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img07F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img07ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img07F);
        }
        
        // 设置配图的frame
        if (_model.image8 != nil && ![_model.image8 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img07F) + padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img08F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img08ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img07F);
        }
        
        // 设置配图的frame
        if (_model.image9 != nil && ![_model.image9 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = CGRectGetMaxX(self.img08F) + padding/2;
            CGFloat pictureViewY = bottomLine + padding/2;
            CGFloat pictureViewW = picW;
            CGFloat pictureViewH = picW;
            self.img09F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img09ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            temp_bottomLine = CGRectGetMaxY(self.img07F);
            bottomLine = CGRectGetMaxY(self.img07F);
        }
    
        if (temp_bottomLine != 0 ) {
            bottomLine = temp_bottomLine;
        }

        // 设置大图
        if (_model.image10 != nil && ![_model.image10 isEqualToString:@""]) {//有配图
            CGFloat pictureViewX = nameLabelX;
            CGFloat pictureViewY = bottomLine + padding;
            CGFloat pictureViewW = sa.screenWidth - nameLabelX - padding;
            CGFloat pictureViewH = 150*sa.ratioHeight;
            self.img10F = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            self.img10ButtonF = CGRectMake(pictureViewX, pictureViewY, pictureViewW, pictureViewH);
            bottomLine = CGRectGetMaxY(self.img10F);
        }
    }
    
    CGFloat timeStampX = nameLabelX;
    CGFloat timeStampY = bottomLine + padding;
    CGSize timeStampSize = [self sizeWithString:_model.timestamp font:TIMETextFont maxSize:CGSizeMake(sa.screenWidth-padding*2, MAXFLOAT)];
    
    CGFloat timeStampW = timeStampSize.width;
    CGFloat timeStampH = timeStampSize.height;
    self.timestampF = CGRectMake(timeStampX, timeStampY, timeStampW, timeStampH);
    
    // 计算行高
    self.cellHeight = CGRectGetMaxY(self.timestampF) + padding;
}

/**
 *  计算文本的宽高
 *
 *  @param str     需要计算的文本
 *  @param font    文本显示的字体
 *  @param maxSize 文本显示的范围
 *
 *  @return 文本占用的真实宽高
 */
- (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName : font};
    // 如果将来计算的文字的范围超出了指定的范围,返回的就是指定的范围
    // 如果将来计算的文字的范围小于指定的范围, 返回的就是真实的范围
    CGSize size =  [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    
    return size;
}
@end